﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using _MetalSlug.MyCharacters.Characters;
using _MetalSlug.Soldiers.Units;
using _MetalSlug.Form.ScreenSystem;
using _MetalSlug.Form.ScreenSystem.Message;
using _MetalSlug.Mission;
using Microsoft.Xna.Framework.Input;
using _MetalSlug.Mementoes;
using _MetalSlug.MyCharacters.Characters.Guns;
using _MetalSlug.MyUtilities;

namespace _MetalSlug.Form.PlayGame
{
    public class PlayGameScreen : MyScreen
    {
        const int MaxMap = 1;
        int currentMap;
        protected int CurrentMap
        {
            get { return currentMap; }
            set { currentMap = value;}
        }

        MyMission CurrentMission;
         
        int GameCore;
        Texture2D HpBar;
        List<Texture2D> CoreTextures;
        List<Texture2D> BulletCountTextures;
        List<Texture2D> HpTextures;

        MissionBuilder currentBuilder;

        public PlayGameScreen()
        {
            LoadContent();
            InitialAttribute();
        }

        public override void LoadContent()
        {
            CoreTextures = GameUtil.LoadTextures("Font/font1/num", 10);
            HpTextures = GameUtil.LoadTextures("Font/font3/num", 10);
            BulletCountTextures = GameUtil.LoadTextures("Font/font2/num", 11);
            HpBar = GameUtil.LoadTexture("hp");
        }

        public override void InitialAttribute()
        {
            CurrentMap = 0;
            TypeOfWindow = TYPE.PLAYGAME;
            GameCore = 0;
            currentBuilder = null;
            GameUtil.GamePivot = new MyPivot();
        }

        public override void Update(GameTime gameTime)
        {
            if (!GameUtil.Audio.IsMuting)
            {
                if (MediaPlayer.State == MediaState.Stopped || MediaPlayer.State == MediaState.Paused)
                {
                    MediaPlayer.Play(GameUtil.Audio.BgMusic);
                }
            }
            else
            {
                MediaPlayer.Pause();
            }

            if (!CurrentMission.isGameOver())
            {
                if (!CheckForEscape())
                {
                    CurrentMission.Update(gameTime);
                    CheckForSwitchingMap();
                }
            }
            else
            {
                this.Send(TYPE.GAMEOVER, MyMessage.TYPE.SHOW, GameCore);
            }

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            
            CurrentMission.Draw(spriteBatch);
            
            spriteBatch.Draw(HpBar, new Rectangle(0, 445, (int)GameUtil.ScreenSize.X, 40), Color.White);

            //draw core ; 
            int CurrentGameCore = GameCore;
            for (int i = 0; i < 6; i++)
            {
                int x = CurrentGameCore % 10;
                CurrentGameCore /= 10;
                spriteBatch.Draw(CoreTextures[x], new Rectangle(10 + (6 - i) * 20, 10, 16, 24), Color.White);
            }

            //draw bullet mount 
            int CurrentBulletCount = CurrentMission.Hero.CurrentGun.CountBullet;
            for (int i = 0; i < 2; i++)
            {
                int x = CurrentBulletCount % 10;
                CurrentBulletCount /= 10;
                if (CurrentMission.Hero.CurrentGun.TypeOfGun != Gun.TYPE.MINIGUN)
                { 
                    spriteBatch.Draw(BulletCountTextures[x], new Rectangle(200 + (2 - i) * 10, 465, 9, 12), Color.White); 
                }
                else
                {
                    spriteBatch.Draw(BulletCountTextures[10], new Rectangle(210, 465, 9, 12), Color.White);
                }
            }

            //draw hp 
            int CurrentHp = CurrentMission.Hero.Hp;
            if (CurrentHp > 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    int x = CurrentHp % 10;
                    CurrentHp /= 10;
                    spriteBatch.Draw(HpTextures[x], new Rectangle(35 + (3 - i) * 10, 455, 20, 23), Color.White);
                }
            }
            
        }

        public void CheckForSwitchingMap()
        {
            // Switch Map
            if (CurrentMission.GetTycoon().IsDead)
            {
                if (CurrentMap < MaxMap)
                {
                    CurrentMap++;
                    this.Send(TYPE.LOADING, MyMessage.TYPE.SHOW, String.Format("{0} {1} {2}", CurrentMission.Hero.Name, CurrentMap, GameCore));
                }
                else
                {
                    this.Send(TYPE.GAMEOVER, MyMessage.TYPE.SHOW, GameCore);
                }
            }
        }

        public int GetGameCore()
        {
            return GameCore;
        }

        public void AddGameCore(int Bonus)
        {
            GameCore += Bonus;
        }

        public Boolean LoadingMission()
        {
            currentBuilder.BuildMission();
            if(currentBuilder.isBuildingComplete())
            {
                CurrentMission = currentBuilder.BuildedMission;
                return true;
            }
            return false;
        }

        public void SetMissionBuilder(String NameOfHero, int Core, int Level)
        {
            if(currentBuilder != null)
            {
                return;
            }

            MissionFactory MissionFactory;
            if (String.IsNullOrEmpty(NameOfHero))
            {
                MissionFactory = new ContinueMissionFactory();
                NameOfHero = CareTaker.GetInstance().GetHeroMemento().Name;
            }
            else
            {

                MissionFactory = new StandardMissionFactory();
                this.CurrentMap = Level;
                this.GameCore = Core;  
            }

            currentBuilder = new MissionBuilder(Level, NameOfHero, MissionFactory);
        }




        public Boolean CheckForEscape()
        {
            if(UserInput.isKeyClicked(Keys.Escape) && CurrentMission.HeroIsAlive())
            {
                CareTaker.GetInstance().CreateMissionMemento(CurrentMission,GameCore);
                this.Send(TYPE.MAINMENU, MyMessage.TYPE.SHOW, null);
                return true;
            }

            return false;   
        }
    }
}
